// Leader/manage/my-pickup/my-pickup.js
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        navH: 50,
        tabs: [
            {
                id:"0", 
                title:"物流发货",
                icon:"http://booking-prod.oss-cn-shenzhen.aliyuncs.com/tata/Leader/order/orderCenter/1.png",
                num:0
            },
            {
                id:"1", 
                title:"顾客提货",
                icon:"http://booking-prod.oss-cn-shenzhen.aliyuncs.com/tata/Leader/order/orderCenter/2.png",
                num:0
            },
            {
                id:"2", 
                title:"全部订单",
                icon:"http://booking-prod.oss-cn-shenzhen.aliyuncs.com/tata/Leader/order/orderCenter/3.png",
                num:0
            }
        ],
        activityId: "2",
        summaryList: [],
        orderList: [],
        pages: [1, 1, 1],
        pageSize: 15,
        status: [3, 4, 0],
        statusResult: [false, false, false],
        activityList: [],
        statusTitle: ["待支付", "待配送", "司机送货中", "待提货", "已提货"],
        searchText: ""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            navH: app.globalData.navHeight ,
            type: options.rs,
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getOrderSummary();
        this.getOrderList();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        const {statusResult, activityId} = this.data;
        if(statusResult[activityId]){
            console.log("已经到底了");
            return ;
        }
        this.getOrderList();//重新调用请求获取下一页数据
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    getOrderSummary: function(){
        wx.showLoading({
            title: "正在加载"
        })
        util.request(api.leader.orderSummary)
        .then(res=>{
            wx.hideLoading();
            const t1 = "tabs[0].num",
                t2 = "tabs[1].num",
                t3 = "tabs[2].num";
            this.setData({
                [t1]: res.result.deliveringCount,
                [t2]: res.result.toBePickupCount,
                [t3]: res.result.totalCount,
            })
        }).catch(err=>{
            wx.hideLoading();
            wx.showToast({
                title: err.message,
                icon: "none"
            })
        })
    },
    inputTyping(e){
        this.setData({
            searchText: e.detail.value
        })
    },
    searchOrder(){
        // console.log(this.data.searchText);
        this.getOrderList(1, this.data.searchText, 1);
    },
    switchTab(e){
        const id = e.currentTarget.dataset.id;
        this.setData({
            activityId: id
        })
        this.getOrderList();
    },
    getOrderList: function(type, queryStr, pageNo){
        wx.showLoading({
            title: "正在加载"
        })
        const { pages, activityId, pageSize, status, orderList, statusResult } = this.data;
        let data = {
            pageNo: pages[activityId],
            pageSize,
            status: status[activityId],
        }
        if(type == 1 && queryStr){
            data.pageNo = pageNo || pages[activityId]
            data.queryStr = queryStr
        }
        else if(type == 2 && queryStr){
            data.pageNo = pageNo || pages[activityId]
            data.memberId = queryStr
        }
        util.request(api.leader.orderList, data)
        .then(res=>{
            wx.hideLoading();
            if(queryStr){
                const page = "pages["+activityId+"]";
                let result = res.result;
                result.forEach(item=> item.statusText = this.statusText(item.status.value - 1))
                this.setData({
                    [page]: pageNo ? pageNo + 1 : pages[activityId] + 1,
                    activityList: result
                })
                return ;
            }
            const param = "orderList["+activityId+"]";
            const page = "pages["+activityId+"]";
            const status = "statusResult["+activityId+"]";
            let data = pages[activityId] == 1 ? [] : orderList[activityId];
            let result = res.result;
            result.forEach(item=> item.statusText = this.statusText(item.status.value - 1))
            const dd = data.concat(result);
            this.setData({
                [param]: dd,
                activityList: dd,
                [page]: dd.length < this.data.pageSize ? pages[activityId] : pages[activityId] + 1,
                [status]: dd.length < this.data.pageSize ? true : false
            })
        }).catch(err=>{
            wx.hideLoading();
            wx.showToast({
                title: err.message,
                icon: "none"
            })
        })
    },
    callPhone(e){
        const phone = e.currentTarget.dataset.phone;
        if(!phone){
            return wx.showToast({
                title: "没有电话哦",
                icon: "none"
            })
        }
        wx.makePhoneCall({
            phoneNumber: phone //仅为示例，并非真实的电话号码
        })
    },
    scanCode(){
        console.log("电泳了吗")
        const _this = this;
        wx.scanCode({
            success (res) {
              console.log(res)
              const id = util.getQueryString(res.path, "memberId");
              _this.getOrderList(2, id, 1);
            }
        })
    },
    statusText(e){
        return this.data.statusTitle[e]
    },  
    copy(e){
        const phone = e.currentTarget.dataset.phone || "";
        if(!phone){
            return wx.showToast({
                title: "没有电话哦",
                icon: "none"
            })
        }
        wx.setClipboardData({
            data: phone,
            success (res) {
                wx.showToast({
                    title: "内容已复制",
                    icon: "none"
                })
                wx.getClipboardData({
                    success(res){
                        console.log("res", res);
                    }
                })
            }
        })
    },
    goToDetail(e){
        const { id, status } = e.currentTarget.dataset;
        wx.navigateTo({
            url: "/Leader/order/order?id="+id + "&status=" + status
        })
    },
    pickUp(e){
        const _this = this;
        wx.showModal({
            title: '',
            content: '真的确认提货吗',
            success (res) {
              if (res.confirm) {
                console.log('用户点击确定')
                _this.orderPickUp(e);
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          });
    },
    receive(e){
        const _this = this;
        wx.showModal({
            title: '',
            content: '真的确认收货吗',
            success (res) {
                if (res.confirm) {
                    console.log('用户点击确定')
                    _this.orderReceive(e);
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        });
    },
    orderReceive(e){
        const id = e.currentTarget.dataset.id;
        wx.showLoading({
            title: "正在加载"
        })
        util.request(api.leader.orderReceive,{
            id
        }, "POST")
        .then(res=>{
            wx.hideLoading();
            wx.showToast({
                title: "确认成功",
                icon: "none"
            })
            this.getOrderList();
        }).catch(err=>{
            wx.hideLoading();
            wx.showToast({
                title: err.message,
                icon: "none"
            })
        })
    },
    orderPickUp(e){
        const id = e.currentTarget.dataset.id;
        wx.showLoading({
            title: "正在加载"
        })
        util.request(api.leader.orderPick,{
            id
        }, "POST")
            .then(res=>{
                wx.hideLoading();
                wx.showToast({
                    title: "确认成功",
                    icon: "none"
                })
                this.getOrderList();
            }).catch(err=>{
            wx.hideLoading();
            wx.showToast({
                title: err.message,
                icon: "none"
            })
        })
    }
})